<template>
  <a-spin :spinning="spinning" tip="加载中...">
    <a-icon slot="indicator" type="loading" style="font-size: 24px" spin />
    <div class="analysis">
      <div class="table mt20">
        <div class="top-filter flex-between mb10">
          <div
            class="strong fs16 mrSemiBold flex-center"
            style="color: #110000"
          >
            <div class="outer">
              <div class="inner"></div>
            </div>
            工作人员管理
          </div>
        </div>
        <a-button v-if="userInfo.role" type="primary" @click="addData()">添加</a-button>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号">
          </el-table-column>
          <el-table-column prop="name" label="工作人员姓名">
          </el-table-column>
          <el-table-column prop="phone" label="工作人员手机号">
          </el-table-column>
          <el-table-column prop="role" label="职责权限">
          </el-table-column>
          <el-table-column label="操作" width="200" v-if="userInfo.role">
            <template slot-scope="scope">
              <el-button @click="addData(scope.row)" type="primary" size="small"
                >编辑</el-button
              >
              <el-button type="danger" @click="deleteFun(scope.row)" size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <a-modal
      :title="modalTitle"
      width="600px"
      :visible="addDialog"
      :okText="'提交'"
      @ok="handleOkAdd"
      @cancel="addDialog = false"
    >
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        label-width="130px"
      >
        <el-form-item
          prop="name"
          label="工作人员姓名"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          prop="phone"
          label="工作人员手机号"
        >
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item
          prop="role"
          label="职责权限"
        >
          <el-select style="width: 100%;" v-model="form.role" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </a-modal>
  </a-spin>
</template>

<script>
import { getAllStaff, addStaff, updateStaff, deleteStaff } from "../../../service";
import moment from "moment";
export default {
  name: "Analysis",
  i18n: require("./i18n"),
  data() {
    return {
      tableData: [],
      spinning: false,
      modalTitle: "",
      form: {
        name: '',
        phone: '',
      },
      addDialog: false,
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'change' }],
        phone: [{ required: true, message: '不能为空', trigger: 'change' }],
        role: [{ required: true, message: '不能为空', trigger: 'change' }]
      },
      roleList: ['暂存人员', '转运人员', '处理人员'],
      userInfo: {}
    };
  },
  created() {
    let userInfo = localStorage.getItem('medicalUserInfo')
    this.userInfo = JSON.parse(localStorage.getItem('medicalUserInfo'))
    this.getAllData();
  },
  methods: {
    getAllData() {
      getAllStaff().then((res) => {
        for (let item of res.data.data) {
          item.recycleTime = moment(item.recycleTime).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        this.tableData = res.data.data;
      });
    },
    addData(params = null) {
      this.modalTitle = params ? "编辑" : "添加";
      if (params) {
        this.form = JSON.parse(JSON.stringify(params));
      } else {
        this.form = {
          name: '',
          phone: '',
          role: '',
        }
      }
      this.addDialog = true;
    },
    handleOkAdd() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.modalTitle === '编辑') {
            // 编辑
            updateStaff(this.form.id, this.form).then((res) => {
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.addDialog = false;
              this.getAllData();
            }
          });
            return
          }
          addStaff(this.form).then((res) => {
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.addDialog = false;
              this.getAllData();
            }
          });
        }
      });
    },
    deleteFun(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStaff(row.id).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.getAllData();
          }
        });
      })
    }
  },
};
</script>

<style lang="less" scoped>
.analysis {
  margin-top: 20px;
  border-radius: 10px;
  width: 100%;

  .right {
    padding: 20px;
  }

  .content {
    background: #ebebeb;
    height: 500px;
    padding: 12px;
    border-radius: 10px;
  }

  .top {
    & > div {
      width: 50%;
      padding: 20px 16px;
    }
    background: #fff;
    display: flex;
    border-radius: 10px;
  }
}

.diy-btn {
  width: 60%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  margin: auto;
  border-radius: 20px;
  cursor: pointer;
}
.diy-btn-hover:hover {
  color: #fff;
  background-color: #9254de;
  border-color: #9254de;
}
.danger-btn-hover:hover {
  color: #fff;
  background-color: #ff7875;
  border-color: #ff7875;
}
.black-btn-hover:hover {
  color: #fff;
  background-color: #655c5c !important;
  border-color: #655c5c;
}
.mt50 {
  margin-top: 50px;
}

.btns {
  width: 60%;
  margin-left: 20%;
  display: flex;
  justify-content: space-around;
  & > div {
    height: 40px;
    line-height: 40px;
    color: #fff;
    text-align: center;
    border-radius: 20px;
    cursor: pointer;
  }
}
.card-each {
  flex: 1;
  height: 80px;
  background: #fff;
  border-radius: 2px;
  padding: 20px;
}
.table {
  background: #fff;
  padding: 20px;
}
</style>
